package cxf.spring.demo.gui;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.validation.ValidationResult;
import cxf.spring.demo.gui.model.UserBean;
import cxf.spring.demo.gui.model.UserBeanValidator;

import javax.swing.*;

/**
 * User: Vlad Vinichenko (akerigan@gmail.com)
 * Date: 24.04.2009
 * Time: 10:02:19
 */
public class UserDetailsDialog extends EmptyDialog {

    UserBean userBean;
    BeanAdapter<UserBean> beanAdapter;
    UserBeanValidator validator;

    JTextField login;
    JTextField firstName;
    JTextField lastName;
    JTextField password;
    JTextField passwordConfirm;

    public UserDetailsDialog(JFrame owner, String title, int width, int height) {
        super(owner, title, width, height);
    }

    protected ValidationResult validateData() {
        return validator.validate(userBean);
    }

    protected void initComponents() {
        userBean = new UserBean();
        beanAdapter = new BeanAdapter<UserBean>(userBean, false);
        validator = new UserBeanValidator();

        login = BasicComponentFactory.createTextField(beanAdapter.getValueModel("login"));
        firstName = BasicComponentFactory.createTextField(beanAdapter.getValueModel("firstName"));
        lastName = BasicComponentFactory.createTextField(beanAdapter.getValueModel("lastName"));
        password = BasicComponentFactory.createPasswordField(beanAdapter.getValueModel("password"));
        passwordConfirm = BasicComponentFactory.createPasswordField(beanAdapter.getValueModel("passwordConfirm"));
    }

    protected JPanel getMainPanel() {
        FormLayout formLayout = new FormLayout("right:pref, 4dlu, 100dlu, 4dlu, pref", "");

        DefaultFormBuilder builder = new DefaultFormBuilder(formLayout);
        builder.setDefaultDialogBorder();

        builder.appendSeparator("User");

        builder.append("Login:", login);
        builder.add(new JButton("Check"));
        builder.nextLine();

        builder.append("First name:", firstName);
        builder.nextLine();

        builder.append("Last name:", lastName);
        builder.nextLine();

        builder.appendSeparator("Password");

        builder.append("Password:", password);
        builder.nextLine();

        builder.append("Confirm password:", passwordConfirm);

        return builder.getPanel();
    }

    public String getLogin() {
        return login.getText();
    }

    public String getFirstName() {
        return firstName.getText();
    }

    public String getLastName() {
        return lastName.getText();
    }

    public static void main(String[] args) {
        new UserDetailsDialog(null, "User details", 500, 300);
    }
}
